home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2956 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.0 KB  |  88 lines

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Locaton of an array?
  5. Date: 24 Jan 1996 08:06:40 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4e5legINNe15@keats.ugrad.cs.ubc.ca>
  8. References: <4d4iqk$hs3@overload.lbl.gov>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4d4iqk$hs3@overload.lbl.gov>,
  12. Mikhail Faiguenblat  <mfaiguen> wrote:
  13. >I am sure this has been answered a million times by now, but could someody help
  14. >me, please?
  15. >
  16. >I have an array in my program, and I need to find out the absolute location in
  17. >memory where this array is located. 
  18. >I have tried to do it this way:
  19. >
  20. >#include <stdio.h>
  21. >
  22. >int main() {
  23. >    char arr[10];
  24. >    int addr;
  25. >
  26. >    printf("sizeof(char *) = %d\n", sizeof(char *));
  27. >    printf("sizeof(int) = %d\n", sizeof(int));
  28. >
  29. >    addr = arr;
  30. >
  31. >    printf("addr = %d\n", addr);
  32. >
  33. >    return 0;
  34. >}
  35. >
  36. >And it did not work:
  37. >
  38. >sizeof(char *) = 4
  39. >sizeof(int) = 4
  40. >addr = 2063810808
  41. >
  42. >Obviously, the computer I run it on does not have 2Gb memory, so I must be
  43. >doing something wrong.
  44. >Can anybody help me?
  45.  
  46. I can see you are posting from a SunOS-based reader, thus I know you are using
  47. a virtual-memory OS.
  48.  
  49. Yes, in a sense your computer does have 2GB memory; more specifically, it has a
  50. large _virtual_ address space, not all of which is populated with "present"
  51. pages.
  52.  
  53. When you have virtual memory, it is possible for your program to be using large
  54. addresses.
  55.  
  56.  
  57. The typical arrangement of a C process on a 32-bit UNIX machine is this.
  58.  
  59. The lowest addresses are used for code and static data. After this is the
  60. uninitialized data segment (called BSS). After the BSS is the "break" address.
  61. Locations above the break address are not present. You can request, via the
  62. brk() system call, that the OS move the break up or down to add or remove
  63. virtual memory from your process.
  64.  
  65. Now, at the opposite end of your memory (which could be 2GB) is the stack,
  66. which grows downward.  The area between the stack and the break is a vast
  67. expanse of addresses which don't correspond to physical memory, and hence don't
  68. use any space. The operating system can instantiate memory in this area by
  69. adding pages to either the stack or the break end, or anywhere in the middle.
  70.  
  71. The fact that your array appears to be located somewhere in the upper part of
  72. the 2nd gigabyte is simply suggests that your stack grows down from the 2GB
  73. limit. You know that the variable is in the stack, because you have declared it
  74. as an auto.
  75.  
  76. The HP-UX OS has a scheme whereby virtual memory is divided into 1GB quadrants.
  77. The first quadrant is for your code. The second is data. The third is for
  78. mapping shared libraries, and the fourth is for the kernel's private use.
  79. Linux (x86) has a similar scheme. Lower 3GB are for code and data, upper gig is
  80. for the kernel.
  81.  
  82. By the way, each process has its own 4GB address space most of which is
  83. independent of other processes. The kernel swaps page tables when it switches
  84. between tasks. (Page tables map virtual memory to physical memory).
  85.  
  86. -- 
  87.  
  88.